package com.skywolf.chem.manager;

import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.live.entity.Video;
import com.skywolf.chem.live.entity.Videoitem;
import com.skywolf.chem.live.param.VideoDto;
import com.skywolf.chem.manager.param.VideoItemDto;
import com.skywolf.chem.live.service.IVideoService;
import com.skywolf.chem.live.service.IVideoitemService;
import com.skywolf.chem.manager.param.VideoQueryDto;
import com.skywolf.chem.tencent.utils.Signature;
import com.skywolf.chem.utils.SysConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

/**
 * Created by pei_pei on 2020/12/11.
 */
@RestController
@RequestMapping("/manager/video")
@Api(value = "管理后台-视频相关", description = "管理后台-视频相关")
public class VideoManagerController {

    @Autowired
    IVideoService videoService;

    @Autowired
    IVideoitemService iVideoitemService;

    @ApiOperation(value = "腾讯云点播获取签名",notes = "腾讯云点播获取签名")
    @RequestMapping(value = "/getSignUrl",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> getSignUrl(HttpServletRequest request) {
        String secretId = SysConfig._this.getProperty("tencent_cdn.secret_id");
        String secretKey = SysConfig._this.getProperty("tencent_cdn.secret_key");
        //得到Sign
        Signature sign = new Signature();
        //个人API密钥中的Secret Id
        sign.setSecretId(secretId);
        //个人API密钥中的Secret Key
        sign.setSecretKey(secretKey);
        sign.setCurrentTime(System.currentTimeMillis() / 1000);
        sign.setRandom(new Random().nextInt(Integer.MAX_VALUE));
        sign.setSignValidDuration(3600 * 24 * 2);
        String signature = null;
        try {
            signature = sign.getUploadSignature();
        } catch (Exception e) {
            System.out.print("获取签名失败");
            e.printStackTrace();
        }
        return RespData.success(signature);
    }


    @ApiOperation(value = "视频下新增课程",notes = "视频下新增课程")
    @RequestMapping(value = "/addVideoItem",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> addVideoItem(HttpServletRequest request, @RequestBody @Validated VideoItemDto param) {
        Videoitem videoitem = new Videoitem();
        BeanUtils.copyProperties(param,videoitem);
        iVideoitemService.save(videoitem);
        return RespData.success();
    }


    @ApiOperation(value = "更新视频下课程",notes = "更新视频下课程")
    @RequestMapping(value = "/updateVideoItem",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> updateVideoItem(HttpServletRequest request, @RequestBody @Validated VideoItemDto param) {
        iVideoitemService.updateVideoItem(param);
        return RespData.success();
    }

    @ApiOperation(value = "删除视频下课程",notes = "删除视频下课程")
    @RequestMapping(value = "/delVideoItem",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> delVideoItem(HttpServletRequest request, @RequestParam(value = "id",required = true) Long id) {
        iVideoitemService.delVideoItem(id);
        return RespData.success();
    }

    @ApiOperation(value = "视频下课程列表",notes = "视频下课程列表")
    @RequestMapping(value = "/videoItemInfo",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<VideoItemDto> videoItemInfo(HttpServletRequest request, @RequestParam(value = "id",required = true) Long id) {
        VideoItemDto dto = iVideoitemService.videoItemInfo(id);
        return RespData.success(dto);
    }

    @ApiOperation(value = "视频下课程列表",notes = "视频下课程列表")
    @RequestMapping(value = "/videoItemList",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<List<VideoItemDto>> videoItemList(HttpServletRequest request, @RequestParam(value = "id",required = true) Long id) {
        List<VideoItemDto> dto = iVideoitemService.getItemList(id);
        return RespData.success(dto);
    }

    @ApiOperation(value = "视频列表",notes = "视频列表")
    @RequestMapping(value = "/videoList",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<VideoDto> videoList(HttpServletRequest request, @RequestBody @Validated VideoQueryDto queryParam) {
        PageVO<VideoDto> page = videoService.pageVideoListManager(queryParam);
        return RespData.success(page);
    }


    @ApiOperation(value = "更新视频",notes = "更新视频")
    @RequestMapping(value = "/updateVideo",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> updateVideo(HttpServletRequest request, @RequestBody @Validated VideoDto param) {
        Video video = new Video();
        BeanUtils.copyProperties(param,video);
        video.setSpeaker(param.getUserName());
        videoService.updateById(video);
        return RespData.success();
    }

    @ApiOperation(value = "添加视频",notes = "添加视频")
    @RequestMapping(value = "/addVideo",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> addVideo(HttpServletRequest request, @RequestBody @Validated VideoDto param) {
        Video video = new Video();
        BeanUtils.copyProperties(param,video);
        video.setSpeaker(param.getUserName());
        videoService.save(video);
        return RespData.success();
    }


    @ApiOperation(value = "根据帖子id，查询帖子详情",notes = "根据帖子id，查询帖子详情")
    @RequestMapping(value = "/getVideoInfo",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<VideoDto> getVideoInfo(@RequestParam(value = "id",required = true) Long id) {
        VideoDto video = videoService.getVideoManagerInfo(id);
        return RespData.success(video);
    }


    @ApiOperation(value = "删除视频",notes = "删除视频")
    @RequestMapping(value = "/delVideoById",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> delVideoById(@RequestParam(value = "id",required = true) Long id) {
        videoService.removeById(id);
        return RespData.success("success");
    }

    @ApiOperation(value = "视频上下架",notes = "视频上下架")
    @RequestMapping(value = "/updateStatus",method = {RequestMethod.POST},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespData<String> updateStatus(HttpServletRequest request, @RequestBody @Validated VideoQueryDto queryParam) {
        Video video = new Video();
        video.setId(queryParam.getId());
        video.setStatus(queryParam.getStatus());
        videoService.updateById(video);
        return RespData.success();
    }


}
